package ink.xiaobaibai.rbac;

import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.SecurityMetadataSource;
import org.springframework.security.access.intercept.AbstractSecurityInterceptor;
import org.springframework.security.access.intercept.InterceptorStatusToken;
import org.springframework.security.web.FilterInvocation;

import javax.servlet.*;
import java.io.IOException;

/**
 * @description: rbac集合过滤器
 * @author: 小白白
 * @create: 2021-05-15
 **/

/**
 * 不要注入IOC,否则会经过两次过滤器
 */
//@Component
public class RbacSecurityFilter extends AbstractSecurityInterceptor implements Filter {

    /**
     * 为什么security已经加了http,
     * 而还需要把此类变成bean呢?
     * 懂了,之前那个将http加入security进行后置处理,是参考 松哥 那本书的;
     * <p>
     * 本次将此类变为bean是直接addAfterFilter()是参考博客的写法。
     * <p>
     * 最终: 将选用博客那种方法
     */

//    @Autowired
    private FilterInvocationSecurityMetadataSourceImpl filterInvocationSecurityMetadataSourceImpl;

    public RbacSecurityFilter(FilterInvocationSecurityMetadataSourceImpl filterInvocationSecurityMetadataSource,
                              AccessDecisionManagerImpl accessDecisionManagerImpl) {
        this.filterInvocationSecurityMetadataSourceImpl = filterInvocationSecurityMetadataSource;
        this.setAccessDecisionManager(accessDecisionManagerImpl);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        FilterInvocation fi = new FilterInvocation(request, response, chain);
        this.invoke(fi);
    }

    private void invoke(FilterInvocation fi) throws IOException, ServletException {
        /**
         *1.fi里面有一个被拦截的url
         *2.里面调用MyInvocationSecurityMetadataSource的getAttributes(Object object)这个方法获取fi对应的所有权限
         *3.再调用MyAccessDecisionManager的decide方法来校验用户的权限是否足够
         **/
        InterceptorStatusToken token = super.beforeInvocation(fi);
        try {
            System.out.println("当前url:" + fi.getRequest().getRequestURL());
            /*执行下一个拦截器*/
            fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
        } finally {
            /**用于实现受保护对象请求完毕后的一些清理工作，
             * 主要是如果在beforeInvocation()中改变了SecurityContext，
             * 则在finallyInvocation()中需要将其恢复为原来的SecurityContext
             **/
            super.finallyInvocation(token);
        }
        super.afterInvocation(token, null);
    }

    @Override
    public void destroy() {

    }

    @Override
    public Class<?> getSecureObjectClass() {
        return FilterInvocation.class;
    }

    /**
     * 返回自定义的Source数据源
     *
     * @return
     */
    @Override
    public SecurityMetadataSource obtainSecurityMetadataSource() {
        return this.filterInvocationSecurityMetadataSourceImpl;
    }

    /**
     * 设置自定义的AccessDecisionManager
     *
     * @param accessDecisionManager
     */
//    @Autowired
    @Override
    public void setAccessDecisionManager(AccessDecisionManager accessDecisionManager) {
        super.setAccessDecisionManager(accessDecisionManager);
    }

}
